﻿using Ninject.Core.Parameters;
using TestBat.IModels.Domain;
using TestBat.IModels.Services;
using TestBat.IViewModels.Workspaces.Results;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Base;
using System.Windows.Input;
using TestBat.ViewModels.Base.Commands;
using Microsoft.Win32;
using TestBat.IInfrastructure.CSV;
using System.Collections.Generic;
using TestBat.IModels.Models;

namespace TestBat.ViewModels.Workspaces.Results
{
    public class ResultScheduleViewModel : ListItemViewModel, IResultScheduleViewModel
    {
        public ResultScheduleViewModel(ISchedule schedule, IResultsService resultsService, IScheduleModel scheduleModel)
        {
            Schedule = schedule;
            this.resultsService = resultsService;
            this.scheduleModel = scheduleModel;
        }

        public override void OnLostFocus()
        {
        }

        public ICommand ExportCommand
        {
            get
            {
                return new RelayCommand(ctx => Export());
            }
        }

        public IScheduleResults ScheduleResults
        {
            get
            {
                if (scheduleResults == null)
                    scheduleResults = resultsService.GetResults(Schedule, false);

                return scheduleResults;
            }
            private set
            {
                scheduleResults = value;
                NotifyPropertyChanged(() => ScheduleResults);
            }
        }

        public IResultTraineeListViewModel ResultTraineeList
        {
            get
            {
                if (resultTraineeList == null)
                    resultTraineeList = DefaultServiceLocator.Get<IResultTraineeListViewModel>(With.Parameters.ConstructorArgument("schedule", Schedule));
                return resultTraineeList;
            }
        }

        public ISchedule Schedule
        {
            get;
            private set;
        }

        private void Export()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = Schedule.Name;
            sfd.DefaultExt = ".csv";
            sfd.Filter = "Comma separated values (.csv)|*.csv";

            bool? result = sfd.ShowDialog();

            if (result == true)
            {
                string filename = sfd.FileName;

                ICSVWriter writer = DefaultServiceLocator.Get<ICSVWriter>();

                string[] headers = new string[] { "Username", "Score (%)" };
                IList<string[]> data = new List<string[]>();

                // add graded
                foreach (ITest t in scheduleModel.GetGradedTests(Schedule))
                {
                    ITestResults r = resultsService.GetResults(t);
                    string correct = r.PercentageCorrect.ToString();
                    data.Add(new string[] { t.Trainee.Username, correct });
                }

                // add not graded
                // TODO
                //foreach (ITrainee t in scheduleModel.GetTraineesTestNotGraded(Schedule))
                //{
                //    string correct = "N.A.";
                //    data.Add(new string[] { t.Username, correct });
                //}

                writer.Write(filename, headers, data);
            }
        }

        private IScheduleResults scheduleResults;

        private IResultTraineeListViewModel resultTraineeList;

        private readonly IResultsService resultsService;
        private readonly IScheduleModel scheduleModel;
    }
}
